﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Twi.COMMON.HTTP
{
    public class ConfigSettings
    {
        private static XmlDocument _doc;
        private static string NodePath = "configuration/appSettings";
        private ConfigSettings() { }

        public static string GetSettingValue(string key)
        {
            try
            {
                XmlNode appSettingsNode = loadConfigDocument().SelectSingleNode(NodePath);

                foreach (XmlNode childNode in appSettingsNode)
                {
                    if (childNode.Attributes == null) continue;
                    if (childNode.Attributes["key"] != null && childNode.Attributes["key"].Value == key)
                    {
                        return childNode.Attributes["value"].Value;
                    }
                }
                return "";
            }
            catch (Exception e)
            { throw e; }
        }

        public static void SetSettingValue(string key, string value)
        {
            XmlDocument doc = loadConfigDocument();
            try
            {
                XmlNode appSettingsNode = doc.SelectSingleNode(NodePath);
                bool exists = false;
                foreach (XmlNode childNode in appSettingsNode)
                {
                    if (childNode.Attributes == null) continue;
                    if (childNode.Attributes["key"] != null && childNode.Attributes["key"].Value == key)
                    {
                        childNode.Attributes["value"].Value = value;
                        exists = true;
                    }
                }
                if (!exists)
                {
                    XmlNode appSettingNode = doc.CreateNode(XmlNodeType.Element, "add", null);
                    appSettingNode.Attributes.Append(CreateAttribute(appSettingNode, "key", key));
                    appSettingNode.Attributes.Append(CreateAttribute(appSettingNode, "value", value));
                    appSettingsNode.AppendChild(appSettingNode);
                }
                doc.Save(getConfigFilePath());
                _doc = doc;
            }
            catch (Exception e)
            { throw e; }
        }
        public static XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;
                XmlAttribute attr = null;
                attr = doc.CreateAttribute(attributeName);
                attr.Value = value;
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }
        public static XmlDocument loadConfigDocument()
        {
            try
            {
                if (_doc == null)
                {
                    _doc = new XmlDocument();
                    _doc.Load(getConfigFilePath());
                }
                return _doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("未找到配置文件", e);
            }
        }
        private static string getConfigFilePath()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\Settings.config";
            if (!File.Exists(filePath))
            {
                filePath = AppDomain.CurrentDomain.BaseDirectory + "\\..\\Settings.config";
                if (!File.Exists(filePath))
                {
                    filePath = AppDomain.CurrentDomain.BaseDirectory + "\\Client\\Settings.config";
                }
            }
            return filePath;
        }
    }
}
